//
//  LoginViewController.m
//  YYZB
//
//  Created by BY_R on 16/3/25.
//
//

#import "LogViewController.h"
#import "UserWebService.h"
#import "RegistViewController.h"
#import "RecoverViewController.h"
#import "NSString+MD5.h"
#import "NSString+MobileNo.h"
#import "HomeViewController.h"
#import "LoadingView.h"
#import "PersonalViewController.h"
#import "UserInfoController.h"
#import "MainViewController.h"
#import "CommitInfoViewController.h"

#import "AppDelegate.h"

@interface LogViewController ()
@property(nonatomic,strong)UserWebService * userWebService;


@end

@implementation LogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.navigationItem.title = @"欢迎加入悦帮";
   self.userWebService = UserWebService.new;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if ([UserInfoController userNo].length) {
        self.userTF.text = [UserInfoController userNo];
    }
}

- (UITabBarController *)tabBarController {
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    return (UITabBarController *)window.rootViewController;
}

- (IBAction)loginBtn:(id)sender {
    

    if ([self.userTF.text isMobileNumber] && self.passwordTF.text.length != 0) {
        
        [LoadingView show];
        [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
        
        [self.userWebService login:self.userTF.text password:[self.passwordTF.text MD5] iphoneID:[[UIDevice currentDevice].identifierForVendor UUIDString] completionHanlder:^(NSArray *items, NSError *error) {
            
            
            [LoadingView dismiss];
            if (error) {
                [LoadingView showToast:[error localizedDescription]];
            }
            //判断账号密码是否正确
            else {
                self.helperType = [items valueForKey:@"HelperType"];
                self.helperStatus = [items valueForKey:@"HelperStatus"];
                
                [[NSUserDefaults standardUserDefaults]setObject:self.helperStatus forKey:@"status"];
                [[NSUserDefaults standardUserDefaults]setObject:self.helperType forKey:@"type"];
                
                NSDictionary * item = (NSDictionary*)items;
                [UserInfoController setUserNo:self.userTF.text];
                [UserInfoController setPassword:[self.passwordTF.text MD5]];
                [UserInfoController setUserID:item[@"UserId"]];
                [UserInfoController setSessionKey:item[@"SessionKey"]];
                
                //判断是否 1为散工； 2为机构工
                if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue] == 1) {
                    //散工登录状态
                    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"status"]intValue] == 0||[[[NSUserDefaults standardUserDefaults]objectForKey:@"status"]intValue] == 3) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            CommitInfoViewController * vc = [[CommitInfoViewController alloc]init];
                            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
                            [UIApplication sharedApplication].keyWindow.rootViewController = navController;
                            
                        });
                        
                    }
                    else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"status"]intValue] == 2){
                        dispatch_async(dispatch_get_main_queue(), ^{
                            UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                            MainViewController * vc = [story instantiateViewControllerWithIdentifier:@"MainViewController"];
                            vc.selectedIndex = 1;
                            vc.delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                            [UIApplication sharedApplication].keyWindow.rootViewController = vc;
//                            [self presentViewController:vc animated:YES completion:^{
                                [LoadingView dismiss];
                                
//                            }];
                            
                        });
                    }
                    else //([[[NSUserDefaults standardUserDefaults]objectForKey:@"status"]intValue] == 2)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                            MainViewController * vc = [story instantiateViewControllerWithIdentifier:@"MainViewController"];
                            vc.selectedIndex = 2;
                            vc.delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                            [UIApplication sharedApplication].keyWindow.rootViewController = vc;
//                            [self presentViewController:vc animated:YES completion:^{
                                [LoadingView dismiss];
                                
//                            }];
                            
                        });
                        
                    }//散工结束
                    
                }
                //机构工直接登录
                else //([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue] == 2)
                {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                        MainViewController * vc = [story instantiateViewControllerWithIdentifier:@"MainViewController"];
                        vc.selectedIndex = 1;
                        vc.delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                        [UIApplication sharedApplication].keyWindow.rootViewController = vc;
//                        [self presentViewController:vc animated:YES completion:^{
                            [LoadingView dismiss];
                            
//                        }];
                        
                    });
                }
                
            } //结束判断账号密码
            
            
            
        }]; // request block 结束
        
    }
    else{
        [LoadingView showToast:@"账号或密码有误"];
    }
}
-(void)addAppsession{
    if ([self.userTF.text isMobileNumber] && self.passwordTF.text.length != 0) {
        
        [self.userWebService AddAPPSession:self.userTF.text Password:[self.passwordTF.text MD5] IMEI:[[UIDevice currentDevice].identifierForVendor UUIDString] completionHanlder:^(NSArray *items, NSError *error) {
            
        }];
        
    }
}
-(void)saveUserinfo{
    
}

- (IBAction)registBtn:(id)sender {
    RegistViewController * vc = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (IBAction)forgetBtn:(id)sender {
    RecoverViewController * vc = [[RecoverViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
